using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace Close.io.API
{
	public class CloseIoFieldListBuilder<T> : Builder
	{
		
		private CloseIoFieldListBuilder _builder;

		public CloseIoFieldListBuilder()
		{
			_builder = new CloseIoFieldListBuilder();
		}

		public CloseIoFieldListBuilder<T> Add<TMember>(Expression<Func<T, TMember>> expression)
		{

			var memberExpression = (MemberExpression)expression.Body;
			var propertyName = memberExpression.Member.Name;
			var fieldName = GetJsonFieldName(typeof (T), propertyName);
			
			_builder = _builder.Add(memberExpression.Member.Name);

			return this;
		}

		public IEnumerable<string> GetFields()
		{
			return _builder.Fields;
		}

		public override string ToString()
		{
			if (_builder.Fields != null && _builder.Fields.Any())
			{
				if(_builder.Fields.Count>1)
					return string.Join(",", _builder.Fields);
				return _builder.Fields.First();
			}
			return string.Empty;
		}
	}

	public class CloseIoFieldListBuilder : Builder
	{
		private readonly List<string> _fields = new List<string>();
		


		public List<string> Fields
		{
			get { return _fields; }
		}

		public CloseIoFieldListBuilder Add(string fieldName)
		{
			Fields.Add(fieldName);
			return this;
		}
	}
}